<template>
  <a-modal :title="`给${positionInfo.name}职位分配猎头`" destroyOnClose :visible="visible" :width="820" :maskClosable="false" @ok="confirm" @cancel="cancelModal(false)" class="PositionDistribution">
    <acct-table :overWriteColumns="columns" :forbidControl="true" :data="data" :pagination="pagination" :loading="loading" :selectedRowKeys="selectedRowKeys" @update="setOptions" @setSelected="setSelected"/>
  </a-modal>
</template>

<script>
import AcctTable from '@/components/Config/AcctTable'
import { postPosition } from '@/api/position'
import { findAllAccount } from '@/api/config'
const columns = [
  {
    title: '姓名',
    dataIndex: 'name',
    key: 'name',
    scopedSlots: { customRender: 'name' }
  },
  {
    title: '电话',
    dataIndex: 'phone',
    key: 'phone'
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    key: 'email'
  }
]
export default {
  name: 'PositionRecResume',
  props: {
    visible: {
      type: Boolean,
      default: false,
      require: true
    },
    positionInfo: { // 职位详细信息
      type: Object,
      required: false,
      default: function () {
        return {}
      }
    }
  },
  components: { AcctTable },
  data () {
    return {
      columns,
      selectedRowKeys: [],
      data: [],
      pagination: {
        total: 0,
        simple: true,
        current: 1,
        pageSize: 15
      }
    }
  },
  watch: {
    visible: {
      handler (newValue, oldValue) {
        if (newValue) {
          this.getList()
        }
      },
      immediate: true
    }
  },
  created () {
  },
  methods: {
    async confirm () {
      if (!this.selectedRowKeys || !this.selectedRowKeys.length) {
        this.$message.warning('请选择至少一个猎头账号')
        return
      }
      let params = {
        positionIds: this.positionInfo.id,
        vendorIds: this.selectedRowKeys.join(',')
      }
      let res = await postPosition(params)
      if (res.code === 200) {
        this.$message.success(res.message)
        this.$emit('close', { type: true })
      } else {
        this.$message.error(res.message)
      }
    },
    cancelModal (flag) {
      this.$emit('close')
    },
    setSelected (selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },
    setOptions (data) {
      this.searchField = data ? data.searchField : {}
      this.pagination.current = data && data.pagination ? data.pagination.current : 1
      this.getList()
    },
    async getList () {
      this.loading = true
      this.selectedRowKeys = []
      let params = {
        notCount: true,
        nids: this.positionInfo.job_vendor_array ? this.positionInfo.job_vendor_array.map(item => item.id).join(',') : '',
        status: 'ENABLE',
        pageSize: this.pagination.pageSize,
        pageNum: this.pagination.current,
        ...this.searchField
      }
      let res = await findAllAccount(params)
      if (res.code === 200) {
        this.data = res.data.items
        this.$set(this.pagination, 'total', res.data.totalCount)
      } else {
        this.$message.error(res.message)
      }
      this.loading = false
    }
  }
}
</script>

<style lang="stylus" scoped>
@import '../../styles/var.styl'
</style>
<style lang="stylus">
.PositionDistribution
  :global(.ant-modal-content .ant-modal-body)
    padding 0
  .ant-modal-footer
    padding 16px 24px
</style>
